package org.example.mq.demo;

import org.example.mq.common.Consumer;
import org.example.mq.common.MqException;
import org.example.mq.mqclient.Channel;
import org.example.mq.mqclient.Connection;
import org.example.mq.mqclient.ConnectionFactory;
import org.example.mq.mqsever.core.BasicProperties;
import org.example.mq.mqsever.core.ExchangeType;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:做渐进主义的改良派,不做完没注意的速成派
 * User: XALEXC
 * Date: 2025-06-29
 * Time: 16:25
 */
// 这个类代表一个消费者
public class DemoConsumer {
    public static void main(String[] args) throws IOException, ClassNotFoundException, MqException, InterruptedException {
        System.out.println("启动消费者!");
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(9090);

        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("testQueue", true, false, false, null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                System.out.println("消费数据开始");
                System.out.println("consumerTag:" + consumerTag);
                System.out.println("basicProperties:" + basicProperties);
                System.out.println("body:" + new String(body));
                System.out.println("消费数据结束");
            }
        });

        while(true){
            // 由于消费者也不知道生产者要生产多少数据
            // 所以这里的循环一直模拟等待消费.
            Thread.sleep(500);
        }
    }
}
